import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:jm_flutter/src/common/utils/i18n.dart';

import '../../config.dart';
import '../../ui/resources/colors.dart';

typedef ShowPosition = EasyLoadingToastPosition;
typedef ShowAnimationStyle = EasyLoadingAnimationStyle;

class ShowTheme {
  static Color? backgroundColor; // 设置背景颜色
  static Color? progressColor; // 设置进度条颜色
  static Color? indicatorColor; // 设置指示器颜色
  static Color? textColor; // 设置文本颜色
  static TextStyle? textStyle; // 设置文本样式
  static List<BoxShadow>? boxShadow; // 设置阴影
  static double? radius; // 设置圆角
  static ShowPosition? position; // 设置位置
  static ShowAnimationStyle? animationStyle; // 设置动画样式
  static EdgeInsets? margin; // 设置外边距
}

class Show {
  static void toast(String str, {duration = 2000, bool i18n = true}) {
    EasyLoading.showToast(str.i18nEnabled(i18n),
        duration: Duration(milliseconds: duration));
  }

  static void snackBar(String title, String str) {
    Get.snackbar(title, str);
  }

  static void success(String str, {bool i18n = true}) {
    EasyLoading.showSuccess(str.i18nEnabled(i18n));
  }

  static void error(String str, {int? duration, bool i18n = true}) {
    EasyLoading.showError(str.i18nEnabled(i18n),
        duration: Duration(milliseconds: duration ?? 2000));
  }

  static void info(String str, {int? duration, bool i18n = true}) {
    EasyLoading.showInfo(str.i18nEnabled(i18n),
        duration: Duration(milliseconds: duration ?? 2000));
  }

  static void loading({String? str, bool i18n = true}) {
    if (str == null) {
      EasyLoading.show(status: "", duration: Duration(seconds: 9999));
    } else {
      EasyLoading.show(
          status: "${str.i18nEnabled(i18n)}...",
          duration: Duration(seconds: 9999));
    }
  }

  static void custom(
    Widget widget, {
    int? duration,
    ShowPosition? position,
    EdgeInsets? margin,
    EdgeInsets? padding,
    Color? backgroundColor,
    double? radius,
  }) {
    EasyLoading.show(
      indicator: widget,
      duration: duration != null ? Duration(milliseconds: duration) : null,
      toastPosition: position,
      margin: margin,
      contentPadding: padding,
      backgroundColor: backgroundColor,
      radius: radius,
    );
  }

  //val should be 0.0 ~ 1.0
  static void progress(double val, String str, {bool i18n = true}) {
    EasyLoading.showProgress(val, status: str.i18nEnabled(i18n));
  }

  // 关闭
  static Future<void> dismiss() async {
    return EasyLoading.dismiss();
  }

  // 关闭
  static Future<void> close() async {
    return EasyLoading.dismiss();
  }

  static void init() {
    Color? textColor = JmConfig.toastTextStyle.color;

    if (ShowTheme.textStyle != null && ShowTheme.textStyle!.color != null) {
      textColor = ShowTheme.textStyle?.color;
    }

    if (ShowTheme.textColor != null) {
      textColor = ShowTheme.textColor;
    }

    EasyLoading.instance.loadingStyle = EasyLoadingStyle.custom; // 设置加载样式
    EasyLoading.instance.backgroundColor =
        ShowTheme.backgroundColor ?? JmColors.toastBg; // 设置背景颜色
    EasyLoading.instance.indicatorColor =
        ShowTheme.indicatorColor ?? textColor; // 设置指示器颜色
    EasyLoading.instance.progressColor =
        ShowTheme.progressColor ?? textColor; // 设置进度条颜色
    EasyLoading.instance.textColor = textColor; // 设置文本颜色

    EasyLoading.instance.textStyle = ShowTheme.textStyle ??
        TextStyle(
            color: textColor,
            fontSize: JmConfig.toastTextStyle.fontSize,
            fontFamily: JmConfig.toastTextStyle.fontFamily,
            fontWeight: JmConfig.toastTextStyle.fontWeight); // 设置文本样式
    EasyLoading.instance.animationStyle =
        ShowTheme.animationStyle ?? EasyLoadingAnimationStyle.scale; // 设置动画样式
    EasyLoading.instance.boxShadow =
        ShowTheme.boxShadow ?? <BoxShadow>[]; // 设置阴影
    EasyLoading.instance.toastPosition =
        ShowTheme.position ?? EasyLoadingToastPosition.center; // 设置位置
    EasyLoading.instance.indicatorType =
        EasyLoadingIndicatorType.ring; // 设置指示器类型
    EasyLoading.instance.radius = ShowTheme.radius ?? 8; // 设置圆角
  }

  // 设置位置 居中
  static void setPositionCenter() {
    EasyLoading.instance.toastPosition = EasyLoadingToastPosition.center;
  }

  // 设置位置 底部
  static void setPositionBottom() {
    EasyLoading.instance.toastPosition = EasyLoadingToastPosition.bottom;
  }

  // 设置位置 顶部
  static void setPositionTop() {
    EasyLoading.instance.toastPosition = EasyLoadingToastPosition.top;
  }

  // 设置位置 默认
  static void setPositionDefault() {
    EasyLoading.instance.toastPosition =
        ShowTheme.position ?? EasyLoadingToastPosition.center;
  }
}
